package com.farm.forms.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.farm.forms.domain.Query;
import com.farm.forms.domain.Title;

import com.farm.forms.dao.QueryDaoInter;
import com.farm.forms.dao.TitleDaoInter;
import com.farm.forms.service.TitleServiceInter;
import com.farm.core.auth.domain.LoginUser;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DataQuery;

/* *
 *功能：结果集合服务层实现类
 *详细：
 *
 *版本：v0.1
 *作者：王东
 *日期：20141124152033
 *说明：
 */
public class TitleServiceImpl implements TitleServiceInter {
	private TitleDaoInter titleDao;
	private QueryDaoInter queryDao;

	@Override
	public Title insertTitleEntity(Title entity, LoginUser user) {
		return titleDao.insertEntity(entity);
	}

	@Override
	public Title editTitleEntity(Title entity, LoginUser user) {
		Title entity2 = titleDao.getEntity(entity.getId());
		entity2.setSort(entity.getSort());
		entity2.setAligntype(entity.getAligntype());
		entity2.setSortable(entity.getSortable());
		entity2.setSorttype(entity.getSorttype());
		entity2.setFixis(entity.getFixis());
		entity2.setWidth(entity.getWidth());
		entity2.setFieldname(entity.getFieldname());
		entity2.setTitle(entity.getTitle());
		entity2.setDictype(entity.getDictype());
		entity2.setDicpara(entity.getDicpara());
		titleDao.editEntity(entity2);
		return entity2;
	}

	@Override
	public void deleteTitleEntity(String id, LoginUser user) {
		// TODO 自动生成代码,修改后请去除本注释
		titleDao.deleteEntity(titleDao.getEntity(id));
	}

	@Override
	public Title getTitleEntity(String id) {
		// TODO 自动生成代码,修改后请去除本注释
		if (id == null) {
			return null;
		}
		return titleDao.getEntity(id);
	}

	@Override
	public DataQuery createTitleSimpleQuery(DataQuery query) {
		// TODO 自动生成代码,修改后请去除本注释
		DataQuery dbQuery = DataQuery
				.init(
						query,
						"FARM_FORMS_TITLES",
						"ID,SORT,QUERYID,ALIGNTYPE,SORTABLE,SORTTYPE,FIXIS,WIDTH,FIELDNAME,TITLE,DICTYPE,DICPARA");
		return dbQuery;
	}

	@Override
	public void initTitles(String queryId, LoginUser currentUser) {
		// 删除当前所有title
		titleDao.deleteEntitys(DBRule.addRule(new ArrayList<DBRule>(),
				"QUERYID", queryId, "="));
		// 创建新的title
		Query queryEntity = queryDao.getEntity(queryId);
		String[] titles = queryEntity.getSelectstr().toUpperCase().split(",");
		int index = 0;
		for (String title : titles) {
			Title titleObj = new Title();
			titleObj.setAligntype("left");
			titleObj.setDictype("0");
			titleObj.setFixis("0");
			titleObj.setQueryid(queryId);
			titleObj.setSort(++index);
			titleObj.setSortable("0");
			titleObj.setSorttype("desc");
			titleObj.setWidth(80);
			String[] titleArray = title.toUpperCase().split("AS");
			if (titleArray.length > 1) {
				titleObj.setFieldname(titleArray[0]);
				titleObj.setTitle(titleArray[1]);
			} else {
				titleObj.setFieldname(title);
				titleObj.setTitle(title);
			}
			titleDao.insertEntity(titleObj);
		}
	}

	// ----------------------------------------------------------------------------------
	public TitleDaoInter getTitleDao() {
		return titleDao;
	}

	public void setTitleDao(TitleDaoInter dao) {
		this.titleDao = dao;
	}

	public QueryDaoInter getQueryDao() {
		return queryDao;
	}

	public void setQueryDao(QueryDaoInter queryDao) {
		this.queryDao = queryDao;
	}

	@Override
	public List<Title> getTitlesByQueryId(String queryId) {
		return titleDao.selectEntitys(DBRule.addRule(new ArrayList<DBRule>(),
				"QUERYID", queryId, "="));
	}

}
